package com.yjs.app.trade.service.service.business.product.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yjs.app.trade.core.base.mybatis.dbutil.DynamicSwitchDataSource;
import com.yjs.app.trade.entity.base.util.Page;
import com.yjs.app.trade.entity.bean.MemberSearch;
import com.yjs.app.trade.entity.bean.ProductNetPrice;
import com.yjs.app.trade.entity.bean.ProductPrice;
import com.yjs.app.trade.service.mapper.business.product.ProductMapper;
import com.yjs.app.trade.service.service.business.product.ProductService;

/**
 * 产品业务层接口具体实现
 * 
 * @author Administrator
 *
 */
@Service("productService")
public class ProductServiceImpl implements ProductService {

	@Autowired
	ProductMapper productMapper;

	@Override
	@DynamicSwitchDataSource(dataSource = "op")
	public Page<?> findBuyerDrugNetPrice(Page<ProductNetPrice> page, ProductNetPrice productNetPrice) {
		productNetPrice.setPage(page);
		List<Map<String, Object>> listMaps = productMapper.findBuyerDrugNetPrice(productNetPrice);
		for (Map<String, Object> map : listMaps) {
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				if (entry.getValue() instanceof Timestamp) {
					map.put(entry.getKey(), new Date(((Timestamp)entry.getValue()).getTime()));
				}
			}
		}
		page.setMaps(listMaps);
		return page;
	}

	@Override
	public Object findBuyerApparNetPrice(Page<ProductNetPrice> page, ProductNetPrice productNetPrice) {
		return null;
	}

	@Override
	public Object findSellerDrugNetPrice(Page<ProductNetPrice> page, ProductNetPrice productNetPrice) {
		productNetPrice.setPage(page);
//		List<Map<String, Object>> listMaps = productMapper.findSellerDrugNetPrice(productNetPrice);
		List<Map<String, Object>> listMaps = productMapper.findBuyerDrugNetPrice(productNetPrice);
		for (Map<String, Object> map : listMaps) {
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				if (entry.getValue() instanceof Timestamp) {
					map.put(entry.getKey(), new Date(((Timestamp)entry.getValue()).getTime()));
				}
			}
		}
		page.setMaps(listMaps);
		return page;
	}

	@Override
	public Object findSellerApparNetPrice(Page<ProductNetPrice> page, ProductNetPrice productNetPrice) {
		return null;
	}

	@Override
	@DynamicSwitchDataSource(dataSource = "op")
	public Page<ProductNetPrice> findDispatcherDrugNetPrice(Page<ProductNetPrice> page, ProductNetPrice productNetPrice) {
		productNetPrice.setPage(page);
//		List<Map<String, Object>> listMaps = productMapper.findDispatcherDrugNetPrice(productNetPrice);
		List<Map<String, Object>> listMaps = productMapper.findBuyerDrugNetPrice(productNetPrice);
		for (Map<String, Object> map : listMaps) {
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				if (entry.getValue() instanceof Timestamp) {
					map.put(entry.getKey(), new Date(((Timestamp)entry.getValue()).getTime()));
				}
			}
		}
		page.setMaps(listMaps);
		return page;
	}

	@Override
	public Object findDispatcherApparNetPrice(Page<ProductNetPrice> page, ProductNetPrice productNetPrice) {
		return null;
	}

	@Override
	@DynamicSwitchDataSource(dataSource = "op")
	public Page<ProductNetPrice> findDispathProductPriceAdjust(Page<ProductNetPrice> page,
			ProductNetPrice productNetPrice) {
		productNetPrice.setPage(page);
		List<Map<String, Object>> listMaps = productMapper.findDispathProductPriceAdjust(productNetPrice);
		for (Map<String, Object> map : listMaps) {
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				if (entry.getValue() instanceof Timestamp) {
					map.put(entry.getKey(), new Date(((Timestamp)entry.getValue()).getTime()));
				}
			}
		}
		page.setMaps(listMaps);
		return page;
	}

	@Override
	@DynamicSwitchDataSource(dataSource = "op")
	public Page<ProductNetPrice> findProductByParam(Page<ProductNetPrice> page, ProductNetPrice productNetPrice) {
		productNetPrice.setPage(page);
		List<Map<String, Object>> listMaps = productMapper.findProductByParam(productNetPrice);
		page.setMaps(listMaps);
		return page;
	}

	@Override
	public Page<?> unionProductAudit(Page<MemberSearch>page, MemberSearch memberSearch) {
		memberSearch.setPage(page);
		List<Map<String, Object>> list = productMapper.unionProductAudit(memberSearch);
		page.setMaps(list);
		return page;
	}

	@Override
	@DynamicSwitchDataSource(dataSource = "op")
	public Page<?> findProductPrice(Page<ProductPrice> page, ProductPrice productPrice) {
		productPrice.setPage(page);
		List<Map<String, Object>> list = productMapper.findProductPrice(productPrice);
		page.setMaps(list);
		return page;
	}

	@Override
	public Map<String, Object> findBargain(String bargainDetailId) {
		
		return productMapper.findBargain(bargainDetailId);
	}

	@Override
	public Map<String, Object> findGMP(String productGmpId) {
		
		return productMapper.findGMP(productGmpId);
	}

	@Override
	public Map<String, Object> findAgencyContract(String productAgentProtocolId) {
		
		return productMapper.findAgencyContract(productAgentProtocolId);
	}

	@Override
	public Map<String, Object> findProductAudit(String productDrugId) {
	
		return productMapper.findProductAudit(productDrugId);
	}

	@Override
	public Object findSellerProductList(Page<ProductNetPrice> page, ProductNetPrice productNetPrice) {
		productNetPrice.setPage(page);
		page.setMaps(productMapper.findSellerProductList(productNetPrice));
		return page;
	}
}
